package com.example.day7animatior;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "TAG";
    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.bt2)
    Button bt2;
    @BindView(R.id.bt3)
    Button bt3;
    @BindView(R.id.bt4)
    Button bt4;
    @BindView(R.id.bt5)
    Button bt5;

    @BindView(R.id.iv)
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.bt5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt1:      //小球回弹（按照int致的变化跳动）
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) iv.getLayoutParams();
                layoutParams.bottomMargin = 500;
                iv.setLayoutParams(layoutParams);
                ValueAnimator valueAnimator = ValueAnimator.ofInt(500, 0, 500, 0);
                valueAnimator.setInterpolator(new BounceInterpolator());
                valueAnimator.setDuration(700);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        Integer animatedValue = (Integer) animation.getAnimatedValue();
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) iv.getLayoutParams();
                        layoutParams.bottomMargin = animatedValue;
                        iv.setLayoutParams(layoutParams);

                    }
                });
                valueAnimator.start();

                break;
            case R.id.bt2:          //float
                ValueAnimator valueAnimator1 = ValueAnimator.ofFloat(0f, 1f, 0.5f, 0f);
                valueAnimator1.setDuration(700);
                valueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float animatedValue = (float) animation.getAnimatedValue();
                        iv.setAlpha(animatedValue);
                    }
                });
                valueAnimator1.start();

                break;
            case R.id.bt3:         //argb
                ValueAnimator valueAnimator2 = ValueAnimator.ofArgb(Color.BLUE, Color.GREEN, Color.RED);
                valueAnimator2.setDuration(700);
                valueAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int animatedValue = (int) animation.getAnimatedValue();
                        iv.setBackgroundColor(animatedValue);
                    }
                });
                valueAnimator2.start();
                break;
            case R.id.bt4:                     //ObjectAnimator
                ObjectAnimator translationX = ObjectAnimator.ofFloat(iv, "translationX", 0, 400, 0, 400);
                translationX.setDuration(1700);
                translationX.start();
                //可自定义动画的对应状态
                translationX.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                    }

                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                    }
                });
                translationX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {

                    }
                });
                break;
            case R.id.bt5:    //组合

                ObjectAnimator translationX1 = ObjectAnimator.ofFloat(iv, "translationX", 0, 200);
                ObjectAnimator translationX2 = ObjectAnimator.ofFloat(iv, "translationY", 500, 0,500,0);
                translationX2.setInterpolator(new BounceInterpolator());
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.play(translationX2).after(translationX1);
                animatorSet.setDuration(5000);
                animatorSet.start();

//                ObjectAnimator translationX1 = ObjectAnimator.ofFloat(iv, "translationX", 0, 200);
//                ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0, 1,0,1);
//                ObjectAnimator x = ObjectAnimator.ofFloat(iv, "rotation", 0,360);
//                AnimatorSet animatorSet = new AnimatorSet();
//                animatorSet.play(translationX1).with(scaleY).after(x);
//                animatorSet.setDuration(5000);
//                animatorSet.start();
                break;

        }
    }
}
